package com.aaa.proxy;


import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;
import java.util.Date;

/**
 * @author ：Teacher陈（86521760@qq.com）
 * @date ：Created in 2022/6/15 9:11
 * @description：基于接口的动态代理工具类
 * @modified By：
 * @version: 1.0
 */
public class ProxyDynamicInterface<T> implements InvocationHandler,ProxyDynamic<T> {
    private  T targetObj;

    public ProxyDynamicInterface(T targetObj) {
        this.targetObj = targetObj;
    }

    public ProxyDynamicInterface() {
    }

    /**
     * @create by: Teacher陈（86521760@qq.com）
     * @description: 获取代理对象
     * @create time: 2022/6/15 9:12
     * @return object
     */
    @Override
    public T getProxyObj() {
        Object proxyInstance = Proxy.newProxyInstance(targetObj.getClass().getClassLoader(), targetObj.getClass().getInterfaces(), this);
        return  (T)proxyInstance;
    }

    @Override
    public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
        //增强处理
        System.out.println("日志："+method.getName()+"执行了"+new Date());
        //执行目标对象的方法
        Object invoke = method.invoke(targetObj, args);
        return invoke;
    }

/*    public static void main(String[] args) {
        //目标对象
        MouseService mouseService= new MouseServiceImpl();
        //获取代理对象
        ProxyDynamicInterface<MouseService> mouseServiceProxyDynamicInterface = new ProxyDynamicInterface<MouseService>(mouseService);
        MouseService proxyObj = mouseServiceProxyDynamicInterface.getProxyObj();
        proxyObj.buyMouse(400);
    }*/


}
